<?php

/**
 * Framework_DB_Utility
 *
 * A collection of database functions to deal with tasks that are not
 * neatly handled by the standard Node or Collection functions.
 *
 * @author Application Support Group
 * @copyright University of Georgia
 * @package BAMF
 * @see Framework_Module
 * @filesource
*/


class Framework_DB_Utility extends Framework_DB {

	public function __construct( $theDsn=FRAMEWORK_DSN ) {
		parent::__construct( $theDsn );
	}
	
	public function __destruct() {
		parent::__destruct();
	}	

	public function executeStoredProcedure ($procName, $args = array()) {
		$_output = false;
		
		$argList = "";
		if (count($args) > 0) {
			$argList = implode(", ", $args);
		}
		
		$sql = "EXECUTE $procName $argList";
		
		$result = $this->db->query( $sql ) ;

		if( Framework_ErrorHandler::isError( $result, __METHOD__."::".__LINE__ )) {
			$_output = $result;
		} else {
			$_output = true;
		}
		
		return $_output;
	}// end executeStoredProcedure


	/**
	 * TODO:  This method worked with PEAR::DB; not sure what it should do or what the MDB2 equivalent is.
	 */
	public function getDbInfo($objType) {
		$_output = false ;
		$_output = $this->db->getListOf( $objType ) ;
		
		return $_output ;
	}

	
	/**
	 * TODO:  This method worked with PEAR::DB; not sure what it should do or what the MDB2 equivalent is.
	 */
	public function getTableInfo($table) {
		$this->db->loadModule('Reverse', null, true);
		return $this->db->tableInfo( $table ) ;
	}
	
	
	public function executeQuery ($sql) {
		return $this->db->query( $sql ) ;
	}// end executeQuery

	public function getAll( $sql ) {
		return $this->db->queryAll( $sql ) ;
	}

	public function getDbSyntax() {
		return $this->db->dbsyntax ;
	}

	public function getColumn($sql) {
		return $this->db->queryCol( $sql ) ;
	}// end getColumn

	public function getFirst($sql) {
		return $this->db->queryOne( $sql ) ;
	}// end getColumn

	public static function unserializeDsn( $dsn ) {
		if( @unserialize( $dsn ) !== FALSE ) {
			$dsn = unserialize( $dsn ) ;
		}
		return $dsn ;
	}
	
	/**
	 * arrayToSqlIn
	 * 
	 * Returns an SQL IN fragment suitable for use in a WHERE clause 
	 * or Framework_DB_DaoParameters object. 
	 * 
	 * @param $anArray An array of simple string or numeric values.
	 * @param $wrapIniSingleQuotes Whether to wrap individual array elements in single quotes.  Defaults to <code>false</code>. 
	 * 
	 * @return mixed A string in the format <code>IN (elem1, elem2, ...)<code> or a PEAR_Error object if <code>$anArray</code> is not an array. 
	 * 
	 */ 
	public function arrayToSqlIn( $anArray, $wrapInSingleQuotes=false ) {

		$_output = null ;

		if( is_array( $anArray )) {

			$arrayKeys = array_keys( $anArray ) ;

			$_output = "IN (" ;

			for( $i=0; $i<count( $arrayKeys ); $i++ ) {
				$nextKey = $arrayKeys[$i] ;
				$nextElem = $anArray[$nextKey] ;

				if( $wrapInSingleQuotes ) {
					$_output .= "'$nextElem'" ;
				} else {
					$_output .= "$nextElem" ;
				}

				if( $i < (count( $arrayKeys ) - 1)) {
					$_output .= "," ;
				}
			}

			$_output .= ")" ;

			if( empty( $anArray )) { $_output = "IN ('')" ; }

		} else {
			$_output = Framework_ErrorHandler::raiseError( "Input value is not an array", __METHOD__."::".__LINE__ ) ;
		}

		return $_output ;

	}// end arrayToSqlIn

}

?>
